﻿using BZhanHengjicvWPF.Common;
using MaterialDesignThemes.Wpf;
using MyToDo.Dtos;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BZhanHengjicvWPF.ViewModels
{
    public class AddMemoViewModel : BindableBase, IDialogHostAware
    {
        public string DialogHostName { get ; set ; }
        public DelegateCommand SaveCommand { get { return new DelegateCommand(Save); } }
        public DelegateCommand CancelCommand { get { return new DelegateCommand(Cancel); } }
        private void Save()
        {
            if (string.IsNullOrEmpty(Mode.Title) || string.IsNullOrEmpty(Mode.Content)) return;
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogParameters param = new DialogParameters();
                param.Add("Value", Mode);
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
            }
        }
        private MemoDto mode;
        public MemoDto Mode
        {
            get { return mode; }
            set { mode = value; RaisePropertyChanged(); }
        }

        private void Cancel()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
        }

        public void OnDialogOpen(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("Value")) Mode = parameters.GetValue<MemoDto>("Value");
            else Mode = new MemoDto();
        }
    }
}
